#define ztree(x)
<div class="box box-primary direct-chat direct-chat-primary">
		<div class="zTree box-header with-border">
			<h4 class="box-title lighter smaller">#(x.title)</h4>
			<div class="box-tools pull-right">
               <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
               </button>
               <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
             </div>
		</div>
		#if(x.search)
		   <div class="input-group input-group-sm" style="padding:10px;">
                <input id="#(x.id)_search" type="text" class="form-control">
                    <span class="input-group-btn">
                      <button type="button" class="btn btn-info btn-flat">搜索</button>
                    </span>
                </input>
       		</div>
		#end
		<div class="zTree box-body">
			<div>
				<ul id="#(x.id)" class="ztree"></ul>
			</div>
		</div>
</div>
#include("/view/common/_ztree.html")
<script type="text/javascript">
var zTree_#(x.id) = null;
var #(x.id)_isFirst = true;
var init_ztree_#(x.id) = function(treeId,pIdKey,zTreeBeforeClick,url) {
	if(!pIdKey){
		pIdKey = "pid";
	}
	var setting = {
			#if(x.async)
			async: {
                enable: true, 
                url:url, 
                autoParam:["id", "pid","level","#(x.extfield??)"], 
                type: 'get',
                dataFilter:filter
             },
			#end
			data: {
				key:{
			          isParent: "isparent",
			    },
				simpleData: {
					enable: true,
					idKey:'id',
					pIdKey: pIdKey
				}
			},
			callback: {
				beforeClick: zTreeBeforeClick,
				#if(x.async)
				onAsyncSuccess: #(x.id)_zTreeOnAsyncSuccess,
				#end
			}
		};
	#if(x.async)
		zTree_#(x.id) = $.fn.zTree.init($("#"+treeId), setting);
		if(#(x.search??false)){
			fuzzySearch("#(x.id)","##(x.id)_search",null,false);
		}
	#else
		$.post(url, function(data){
			if(data.state=='ok'){
				zTree_#(x.id) = $.fn.zTree.init($("#"+treeId), setting, data.data);
				if(#(x.search??false)){
					fuzzySearch("#(x.id)","##(x.id)_search",null,false);
				}
				var node =  zTree_#(x.id).getNodeByParam('level', 0);
				zTree_#(x.id).selectNode(node); // 选择节点
				#if(x.expandFirst??false)
				zTree_#(x.id).expandNode(node, true, false, true);
				#end
				zTreeBeforeClick(null,  node, null);
			}
			else{
				layer.msg('数据加载失败!', {icon: 5});
			}
		}, "json");
	
	#end
	$(".zTree.box-body").height($(window).height()-($(".zTree.box-header").height()+51)+125);
}

function #(x.id)_zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
	if (#(x.id)_isFirst){
	      var zTree = $.fn.zTree.getZTreeObj(treeId);
	      var nodeList = zTree.getNodes();
	      //获取根节点个数,getNodes获取的是根节点的集合
	      #if(x.expandFirst??true)
	          zTree.expandNode(nodeList[0], true, false, true);
	      #end
	      zTree.selectNode(nodeList[0]);
	      zTree.setting.callback.beforeClick(null,nodeList[0], null);
	      #(x.id)_isFirst= false;
	  }
};

function filter(treeId, parentNode, childNodes) {
    return childNodes;
}

var addNodes = function(tree,parentNode,node){
	if(tree){
		tree.addNodes(parentNode,node);
	}
}


var delNodes = function(tree,rowids,key){
	if(key==undefined) key = "id";
	for(var i =0;i<rowids.length;i++){
		var node = tree.getNodeByParam(key,rowids[i], null);
		tree.removeNode(node);
	}
}

var updateNodes = function(tree,id,node){
	var oldnode = tree.getNodeByParam("id",id, null);
	oldnode.name=node.name;
	tree.updateNode(oldnode);
}

$(function(){
	init_ztree_#(x.id)("#(x.id)","#(x.pIdKey)",#(x.beforclick),"#(x.url)");
});
</script>
#end